<?php
/**
 * Created by PhpStorm.
 * User: ZZF
 * Date: 2017/8/22
 * Time: 14:27
 */

namespace app\admin\model;
use think\Model;
class GoodsTypeModel extends Model{

    /**
     * 后置商品操作
     */
    public function afterSave($goods_id){
        //商品相册
        if(!empty(input('post.goods_images'))){
            $goodsImagesArr = db('GoodsImages')->where(['goods_id'=>$goods_id])->select();
            //删除图片
            foreach($goodsImagesArr as $item){
                if(!in_array($item['image'],input('post.goods_images'))){
                    $goodsImagesDel[] = $item['id'];
                }
            }
            if(!empty($goodsImagesDel)){
                db('GoodsImages')->delete(implode(',',$goodsImagesDel));
            }

            //添加图片
            foreach($goodsImagesArr as $item){
                $goodsImages[] = $item['image'];
            }
            foreach(input('post.goods_images') as $item){
                if(!in_array($item,$goodsImages)){
                    $goodsImagesAdd[] = [
                        'goods_id' => $goods_id,
                        'image' => $item,
                    ];
                }
            }
            if(!empty($goodsImagesAdd)){
                db('GoodsImages')->saveAll($goodsImagesAdd);
            }
        }

        // 商品规格价钱处理
        $goodsAttrPrice = db('GoodsAttrPrice');
        $goodsAttrPrice->where(['goods_id'=>$goods_id])->delete(); // 删除原有的价格规格对象
        if(!empty($_POST['item'])){
            foreach($_POST['item'] as $k => $v)
            {
                // 批量添加数据
                $v['price'] = trim($v['price']);
                $dataList[] = array('goods_id'=>$goods_id,'key'=>$k,'key_name'=>$v['key_name'],'price'=>$v['price'],'vip_price'=>$v['vip_price'],'cost_price'=>$v['cost_price'],'store_count'=>$v['store_count']);
            }
            $goodsAttrPrice->saveAll($dataList);
        }
        // 刷新商品库存
        refresh_stock($goods_id);


//        //商品属性
//        if(!empty(I('post.attr_id'))){
//            $goodsAttrArr = M('GoodsAttr')->where(['goods_id'=>$goods_id])->select();
//            //删除属性或更新属性加价
//            foreach($goodsAttrArr as $item){
//                $in_array = false;
//                foreach(I('post.attr_id') as $key=>$attr_id){
//                    $attr_value = I('post.attr_value')[$key];
//                    if(!$attr_value){
//                        continue;
//                    }
//                    $attr_price = I('post.attr_price')[$key] ? I('post.attr_price')[$key] : 0;
//                    //如果数据相同
//                    if($item['goods_id'] == $goods_id && $item['attr_id'] == $attr_id && $item['attr_value'] == $attr_value){
//                        $in_array = true;
//                        //如果属性加价数据不相同，则更新
//                        if($item['attr_price'] != I('post.attr_price')[$key]){
//                            M('GoodsAttr')->where(['id'=>$item['id']])->save(['attr_price'=>$attr_price]);
//                        }
//                        break;
//                    }
//                }
//                if(!$in_array){
//                    $goodsAttrDel[] = $item['id'];
//                }
//            }
//            if(!empty($goodsAttrDel)){
//                M('GoodsAttr')->delete(implode(',',$goodsAttrDel));
//            }
//            //添加属性
//            foreach(I('post.attr_id') as $key=>$attr_id){
//                $attr_value = I('post.attr_value')[$key];
//                if(!$attr_value){
//                    continue;
//                }
//                $attr_price = I('post.attr_price')[$key] ? I('post.attr_price')[$key] : 0;
//                $in_array = false;
//                foreach($goodsAttrArr as $item){
//                    if($item['goods_id'] == $goods_id && $item['attr_id'] == $attr_id && $item['attr_value'] == $attr_value){
//                        $in_array = true;
//                        break;
//                    }
//                }
//                if(!$in_array){
//                    $goodsAttrAdd[] = [
//                        'goods_id' => $goods_id,
//                        'attr_id' => $attr_id,
//                        'attr_value' => $attr_value,
//                        'attr_price' => $attr_price
//                    ];
//                }
//            }
//            if(!empty($goodsAttrAdd)){
//                M('GoodsAttr')->addAll($goodsAttrAdd);
//            }
//
//        }
    }



    /**
     * 获取 规格的 笛卡尔积
     * @param $goods_id 商品 id
     * @param $attr_arr 笛卡尔积
     * @return string 返回表格字符串
     */
    public function getSpecInput($goods_id, $attr_arr)
    {
        // 排序
        foreach ($attr_arr as $k => $v)
        {
            $attr_arr_sort[$k] = count($v);
        }
        asort($attr_arr_sort);
        foreach ($attr_arr_sort as $key =>$val)
        {
            $attr_arr2[$key] = $attr_arr[$key];
        }


        $clo_name = array_keys($attr_arr2);
        $attr_arr2 = combineDika($attr_arr2); //  获取 规格的 笛卡尔积                 

        $spec = db('GoodsAttribute')->value('id,name'); // 规格表
        $specItem = db('GoodsAttrValue')->value('id,value,attr_id');//规格项
        $keySpecGoodsPrice = db('GoodsAttrPrice')->where(['goods_id'=>$goods_id])->value('key,key_name,price,vip_price,store_count,cost_price');//规格项
        $str = "<div class='col-sm-7 col-md-offset-1'><table class='table table-bordered text-center' id='spec_input_tab'>";
        $str .="<tr>";
        // 显示第一行的数据
        foreach ($clo_name as $k => $v)
        {
            $str .=" <td><b>{$spec[$v]}</b></td>";
        }
        $str .="<td><b>普通价格</b></td>
                <td><b>VIP价格</b></td>
             </tr>";
        // 显示第二行开始 
        foreach ($attr_arr2 as $k => $v)
        {
            $str .="<tr>";
            $item_key_name = array();
            foreach($v as $k2 => $v2)
            {
                $str .="<td>{$specItem[$v2][value]}</td>";
                $item_key_name[$v2] = $spec[$specItem[$v2]['attr_id']].':'.$specItem[$v2]['value'];
            }
            ksort($item_key_name);
            $item_key = implode('_', array_keys($item_key_name));
            $item_name = implode(' ', $item_key_name);

            $keySpecGoodsPrice[$item_key][price] ? false : $keySpecGoodsPrice[$item_key][price] = 0; // 价格默认为0
            $keySpecGoodsPrice[$item_key][store_count] ? false : $keySpecGoodsPrice[$item_key][store_count] = 0; //库存默认为0
            $str .="<td><input name='item[$item_key][price]' value='{$keySpecGoodsPrice[$item_key][price]}' onkeyup='this.value=this.value.replace(/[^\d.]/g,\"\")' onpaste='this.value=this.value.replace(/[^\d.]/g,\"\")' /></td>";
            $str .="<td><input name='item[$item_key][vip_price]' value='{$keySpecGoodsPrice[$item_key][vip_price]}' onkeyup='this.value=this.value.replace(/[^\d.]/g,\"\")' onpaste='this.value=this.value.replace(/[^\d.]/g,\"\")' /></td>";
            $str .="<input type='hidden' name='item[$item_key][key_name]' value='$item_name' />";
            $str .="</tr>";
        }
        $str .= "</table></div>";
        return $str;
    }
    
}